package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.pojo.entiry.Attribute;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 属性接口的实现类
 */

@Slf4j
@Service
public class AttributeServiceImpl implements IAttributeService {

    @Autowired
    private AttributeMapper attributeMapper;

    @Override
    public void addNewAttribute(AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【新增属性】的业务，参数：{}", attributeAddNewParam);

        // 不用检查属性重名，因为表中有不同模板的属性，包重名的
        // 执行新增
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewParam, attribute);
        attribute.setIsAllowCustomize(0);
        attribute.setGmtCreate(LocalDateTime.now());
        attribute.setGmtModified(LocalDateTime.now());
        int rows = attributeMapper.insert(attribute);
        if (rows != 1) {
            String message = "新增属性失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("新增属性完成！");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据属性ID删除属性】的业务，属性ID：{}", id);

        // 检查属性是否存在
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Integer countById = attributeMapper.selectCount(queryWrapper);
        if (countById == 0) {
            String message = "删除属性失败，属性数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 执行删除
        int rows = attributeMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除属性失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateById(Long id, AttributeUpdateInfoParam attributeUpdateInfoParam) {
        log.debug("开始处理【修改属性详情】的业务，ID：{}，新的属性数据：{}", id, attributeUpdateInfoParam);

        // 检查数据是否存在
        AttributeStandardVO standard = attributeMapper.getStandard(id);
        if (standard == null) {
            String message = "修改属性详情失败，属性数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 执行修改
        Attribute attribute = new Attribute();
        attribute.setId(id);
        BeanUtils.copyProperties(attributeUpdateInfoParam, attribute);
        int rows = attributeMapper.updateById(attribute);
        if (rows != 1) {
            String message = "修改属性详情失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID获取属性详情】的业务，参数ID：{}", id);
        AttributeStandardVO standard = attributeMapper.getStandard(id);
        if (standard == null) {
            String message = "根据ID获取属性详情失败，属性数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standard;
    }


    @Override
    public PageData<AttributeListItemVO> listByAttributeTemplateId(Long id) {
        log.debug("开始处理【根据属性模板ID查询属性列表】的业务，属性模板ID：{}", id);
        Integer pageNo = 1;
        Integer pageSize = Integer.MAX_VALUE;
        PageHelper.startPage(pageNo, pageSize);
        List<AttributeListItemVO> list = attributeMapper.listByAttributeTemplateId(id);
        PageInfo<AttributeListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AttributeListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }


}
